import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/cacheHelper.dart';

import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/danger2.dart';

import '../../widgets/inputWidget.dart';
import '../../widgets/signatureCard2.dart';

import '../../helper/Helper.dart';
import 'danger_deal.dart';
import 'danger_deal_way.dart';

class Danger2InfoPage extends StatefulWidget {
  Danger2InfoPage(this.cb1, this.added);
  final Danger2 cb1;
  final VoidCallback added;

  @override
  Danger2InfoPageState createState() => new Danger2InfoPageState();
}

class Danger2InfoPageState extends State<Danger2InfoPage> {
  final String now = Helper.DateTimeNowString();

  bool ifLoad = false;
  late Danger2 s1;

  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData() async {
    var xx = await Danger2.GetByID(widget.cb1.ID);

    setState(() {
      ifLoad = true;
      s1 = xx!;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      // appBar: AppBar(
      //   title: Text("问题处理登记单"),
      // ),

      body: ifLoad
          ? CleanInfoBody(context)
          : Center(
              child: SpinKitCircle(
              color: Colors.blue,
            )),
    );
  }

  CleanInfoBody(BuildContext context) => SingleChildScrollView(
        child: CleanInfoFields(context),
      );

  String getSS() {
    String bz2 = "";
    if (s1.State == 0) bz2 = bz2 + "待处理";
    if (s1.State == 1) bz2 = bz2 + "待审核";
    if (s1.State == 2) bz2 = bz2 + "已完成";
    return bz2;
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    list3.add(InputWidget.getInfo("标题", s1.Title));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo2("内容简述", s1.Content));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo("登记时间", Helper.DateTimeFormat(s1.CreateTM)));
    list3.add(InputWidget.getDivider());
    list3.add(
        InputWidget.getInfo("登记人员", CacheHelper.getUserName(s1.CreateUser)));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getInfo("当前状态", getSS()));
    list3.add(InputWidget.getDivider());

    if (s1.State >= 1) {
      list4.add(InputWidget.getInfo2("整改措施", s1.Deal));
      list4.add(InputWidget.getDivider());
      list4.add(InputWidget.getUserTile(context, "整改责任人", s1.DealUser));
      list4.add(InputWidget.getDivider());
      list4.add(InputWidget.getInfo("填报时间", Helper.DateTimeFormat(s1.DealTM)));
      list4.add(InputWidget.getDivider());
    }

    if (s1.State >= 2) {
      list5.add(InputWidget.getInfo2("整改完成情况", s1.DealResult));
      list5.add(InputWidget.getDivider());
      //list4.add(InputWidget.getInfo("整改验收时间",Helper.DateTimeFormat(s1.CheckTM)));
      //list4.add(InputWidget.getDivider());
      //list4.add(InputWidget.getInfo("验收确认签字", CacheHelper.getUserName(s1.DealUser) )   );
      list5.add(InputWidget.getQMTile2(
          "验收确认签字", s1.State >= 2, s1.CheckQM, s1.CheckQMTM));
      list5.add(InputWidget.getDivider());
    }

    // if (s1.State >= 2){
    //   list5.add(InputWidget.getQMTile2(
    //       "处置审核", true, s1.CheckQM, s1.CheckQMTM));
    // }

    list1.add(InputWidget.inCard2(list3));
    list1.add(InputWidget.inCard2(list4));
    list1.add(InputWidget.inCard2(list5));

    if (s1.State == 0)
      list2.add(
          InputWidget.getButton("整改措施", () => onTapButton(context, "整改措施")));
    if (s1.State == 1)
      list2.add(InputWidget.getButton(
          "整改结果验收", () => onTapButton(context, "整改结果验收")));
    // if (s1.State==2)
    //   list2.add(InputWidget.getButton("整改验收签字", () => onTapButton(context, "整改验收签字")));

    list1.add(InputWidget.inCard2(list2));
    return list1;
  }

  void onTapButton(BuildContext context, String xx) {
    if (xx == "整改措施") {
      RouteHelper.ShowPage(context, Danger2DealWayPage(s1, loadData));
    }

    if (xx == "整改结果验收") {
      RouteHelper.ShowPage(context, Danger2DealPage(s1, loadData));
    }

    // if (xx == "处置结果审核") {
    //    showPanel(context, signIt1, xx);
    // }
  }

  signIt1(String imgId) async {
    s1.State = 2;
    s1.CheckQMTM = Helper.DateTimeNowString();
    s1.CheckQM = imgId;

    await Danger2.Update(s1);
    loadData();
    //await Helper.sendMessage("审核通知", "18884230190");
  }

  void showPanel(
      BuildContext context, SignaturedCallback checkIt, String title) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SignatureCard2Page(checkIt, title);
      },
    );
  }

  CleanInfoFields(BuildContext context) => Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: getWidgetList(context),
        ),
      );
}
